home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / BITMAP.ZIP / BMFILE.C < prev    next >
C/C++ Source or Header  |  1993-04-19  |  6KB  |  267 lines

  1. /*
  2. **    $id: ssvcid bmfile.c 1.2 08/03/92 10:00 am$
  3. **        This file contains the functions needed to support reading and writing
  4. **    bitmaps from and to Windows 3.0 bitmap files, as well as for reading in
  5. **    icon files.
  6. **
  7. **    (C) 1991-3 Larry Widing
  8. */
  9. #include    <windows.h>
  10. #include    <malloc.h>
  11. #include    <dos.h>
  12. #include    <stdlib.h>
  13. #include    "bitmaps.h"
  14. #include    "bmmanip.h"
  15.  
  16. /*
  17. ** HDIB                                                handle of created DI bitmap
  18. ** ReadBitmapFile(const char *filename);    name of file to load
  19. **
  20. **    This function will read the passed file in, and create a device
  21. **    dependant bitmap, returning the handle to the created bitmap to the
  22. **    caller.
  23. **
  24. ** Modification History:
  25. ** 09/06/91  LCW  Created
  26. */
  27. HDIB
  28. ReadBitmapFile(const char *filename)
  29. {
  30.     HFILE                    file;
  31.     int                    rc;
  32.     int                    block;
  33.     long                    size;
  34.     HDIB                    hdata = (HANDLE)NULL;
  35.     char HUGE            *pdata;
  36.     char HUGE            *ptr;
  37.     BITMAPFILEHEADER    bfHdr;
  38.     OFSTRUCT                ofs;
  39.  
  40.     /*
  41.     **    1. Open file
  42.     */
  43.     file = OpenFile((LPSTR)filename, (LPOFSTRUCT)&ofs, OF_READ | OF_SHARE_DENY_WRITE);
  44.     if (file != -1)
  45.     {
  46.         /*
  47.         **    2. Read in BITMAPFILEHEADER and verify that this is a bitmap file
  48.         */
  49.         rc = _lread(file, (LPSTR)&bfHdr, sizeof(BITMAPFILEHEADER));
  50.         if (rc == sizeof(BITMAPFILEHEADER)
  51.             || bfHdr.bfType == ('B' + ('M' << 8)))
  52.         {
  53.             /*
  54.             **    2.1. Verify that the bfSize field is correct
  55.             */
  56.         {
  57.             LONG    _offset = _llseek(file, 0L, 1);
  58.             LONG    _size = _llseek(file, 0L, 2);
  59.             _llseek(file, _offset, 0);
  60.             if (bfHdr.bfSize != _size)
  61.                 bfHdr.bfSize = _size;
  62.         }
  63.  
  64.             /*
  65.             **    3. Allocate storage for packed DIB
  66.             */
  67.             size = bfHdr.bfSize - sizeof(BITMAPFILEHEADER);
  68.             hdata = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, size);
  69.             if (hdata != (HANDLE)NULL)
  70.             {
  71.                 rc = -1;
  72.                 pdata = (char HUGE *)GlobalLock(hdata);
  73.                 if (pdata != NULL)
  74.                 {
  75.                     /*
  76.                     **    4. Read in DIB header and bits into packed-DIB buffer
  77.                     */
  78.                     block = 16 * 1024;    /* size of chunks to read in */
  79.                     ptr = pdata;
  80.  
  81.                     while (size > 0)
  82.                     {
  83.                         if (size < (long)block)
  84.                         {
  85.                             block = (int)size;
  86.                         }
  87.  
  88.                         if (_lread(file, (LPSTR)ptr, block) != block)
  89.                         {
  90.                             ErrorBox("ReadBitmapFile(): Error reading BMP file");
  91.                             break;
  92.                         }
  93.  
  94.                         size -= (long)block;
  95. #if    defined(__TSC__)
  96.                         if ((FP_OFF(ptr) + block) == 0)
  97.                         {
  98.                             ptr = MK_FP(FP_SEG(ptr) + 8, 0);
  99.                         }
  100.                         else
  101.                         {
  102.                             ptr += block;
  103.                         }
  104. #else
  105.                         ptr += block;
  106. #endif
  107.                     }
  108.  
  109.                     if (size == 0)
  110.                     {
  111.                         rc = 0;
  112.                     }
  113.                     GlobalUnlock(hdata);
  114.                 }
  115.                 else
  116.                 {
  117.                     ErrorBox("ReadBitmapFile(): Error locking packed DIB memory");
  118.                 }
  119.  
  120.                 if (rc < 0)
  121.                 {
  122.                     GlobalFree(hdata);
  123.                     hdata = (HANDLE)NULL;
  124.                 }
  125.             }
  126.             else
  127.             {
  128.                 ErrorBox("ReadBitmapFile(): Unable to allocate memory for packed DIB");
  129.             }
  130.         }
  131.         else
  132.         {
  133.             ErrorBox("ReadBitmapFile(): Error reading BITMAPFILEHEADER");
  134.         }
  135.         _lclose(file);
  136.     }
  137.     else
  138.     {
  139.         ErrorBox("ReadBitmapFile(): Unable to open bitmap file");
  140.     }
  141.  
  142.     return hdata;
  143. }
  144.  
  145. /*
  146. ** int
  147. ** WriteBitmapFile(
  148. **        const char *filename,    name of file to load
  149. **        const HANDLE hbm);        handle to packed DIB
  150. **
  151. **    This function will write the passed packed Device Independant Bitmap
  152. **    to the specified file.
  153. **
  154. ** Modification History:
  155. ** 09/06/91  LCW  Created
  156. */
  157. int
  158. WriteBitmapFile(const char *filename, const HANDLE hbm)
  159. {
  160.     int                    file;
  161.     int                    rc = -1;
  162.     int                    block;
  163.     long                    size;
  164.     char HUGE            *ptr;
  165.     BITMAPFILEHEADER    bfHdr;
  166.     LPBITMAPINFO        bmi;
  167.     OFSTRUCT                ofs;
  168.  
  169.     /*
  170.     **    1. Open output file
  171.     */
  172.     file = OpenFile((LPSTR)filename, (LPOFSTRUCT)&ofs, OF_CREATE | OF_WRITE);
  173.     if (file != -1)
  174.     {
  175.         /*
  176.         **    2. Lock memory resource
  177.         */
  178.         bmi = (LPBITMAPINFO)GlobalLock(hbm);
  179.         if (bmi != NULL)
  180.         {
  181.             /*
  182.             **    3. Create BITMAPFILEHEADER and write to file
  183.             */
  184.             bfHdr.bfType = ('B' + ('M' << 8));
  185.             bfHdr.bfSize = sizeof(BITMAPFILEHEADER) + GlobalSize(hbm);
  186.             bfHdr.bfReserved1 = 0;
  187.             bfHdr.bfReserved2 = 0;
  188.             bfHdr.bfOffBits = sizeof(BITMAPFILEHEADER)
  189.                 + (DWORD)(DIBitmapBits(bmi) - (LPSTR)bmi);
  190.             if (_lwrite(file, (LPSTR)&bfHdr, sizeof(bfHdr)) == sizeof(bfHdr))
  191.             {
  192.                 /*
  193.                 **    4. Write out DIB header and packed bits to file
  194.                 */
  195.                 size = GlobalSize(hbm);
  196.                 ptr = (char HUGE *)bmi;
  197.                 block = 16 * 1024;    /* size of chunks to write out */
  198.  
  199.                 while (size > 0)
  200.                 {
  201.                     if (size < (long)block)
  202.                     {
  203.                         block = (int)size;
  204.                     }
  205.  
  206.                     if (_lwrite(file, (LPSTR)ptr, block) != block)
  207.                     {
  208.                         ErrorBox("WriteBitmapFiile(): Error writing DIB");
  209.                         break;
  210.                     }
  211.  
  212.                     size -= (long)block;
  213. #if    defined(__TSC__)
  214.                     if ((FP_OFF(ptr) + block) == 0)
  215.                     {
  216.                         ptr = MK_FP(FP_SEG(ptr) + 8, 0);
  217.                     }
  218.                     else
  219.                     {
  220.                         ptr += block;
  221.                     }
  222. #else
  223.                     ptr += block;
  224. #endif
  225.                 }
  226.  
  227.                 if (size == 0)
  228.                 {
  229.                     rc = 0;
  230.                 }
  231.             }
  232.             else
  233.             {
  234.                 ErrorBox("WriteBitmapFile(): Error writing BITMAPFILEHEADER");
  235.             }
  236.  
  237.             GlobalUnlock(hbm);
  238.         }
  239.         else
  240.         {
  241.             ErrorBox("WriteBitmapFile(): Error locking bitmap into memory");
  242.         }
  243.         _lclose(file);
  244.     }
  245.     else
  246.     {
  247.         ErrorBox("WriteBitmapFile(): Error opening output file");
  248.     }
  249.  
  250.     if (rc != 0)
  251.     {
  252.         unlink(filename);
  253.     }
  254.     
  255.     return rc;
  256. }
  257.  
  258. /*
  259. **    Modification History
  260. **    --------------------
  261. **    $lgb$
  262. ** 10/15/91     Larry Widing   Initial version for Win Tech Journal Article.
  263. ** 02/10/92     Larry Widing   Added HDIB type.
  264. ** 08/03/92     Larry Widing   
  265. **    $lge$
  266. */
  267.